package cn.melon.view.admin.roll;

import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.melon.mod.roll.RollInfo;
import cn.melon.mod.roll.RollRequest;
import cn.melon.view.admin.roll.ctr.RollPanelCtr;
import cn.melon.view.admin.roll.ctr.RollTableMouse;

public class RollJPanel extends JPanel implements IUpdateRollTable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String majorNames[] = { "计算机科学与技术", "经济管理", "机械", "电子" };
    private JComboBox<String> majorName;
    
    private String claNames[] = { "大二（一）", "大二（二）", "大二（三）", "大二（四）" };
    private JComboBox<String> claName;
    
    private JTextField Id;
    private JTextField name;
    private JButton queryButton;
    private JButton addButton;
    
    public List<RollInfo> infoList;
    public RollTableMouse mouseListener = new RollTableMouse(this);
    
    private String[] tableColName = {"学号","姓名","专业","年级","身份证号","婚姻状况"};
    private JTable table;
    DefaultTableModel tableModel;
    RollPanelCtr listener = new RollPanelCtr(this);

    private RollRequest request = new RollRequest(this);
    /**
     * Create the panel.
     */
    public RollJPanel() {
        initialize();
        request.getAllRollInfo();
    }
    
    private void initialize() {
        this.setSize(1200, 1000);
        this.setBackground(Color.black);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel topPanel = new JPanel();
        topPanel.setSize(1200, 50);
        topPanel.setBackground(Color.darkGray);

        JPanel major = new JPanel();
        JLabel majorText = new JLabel("专业:");
        majorName = new JComboBox<String>(majorNames);
        major.add(majorText);
        major.add(majorName);

        JPanel cla = new JPanel();
        JLabel claText = new JLabel("班级:");
        claName = new JComboBox<String>(claNames);
        cla.add(claText);
        cla.add(claName);

        JPanel studentId = new JPanel();
        JLabel studentIdText = new JLabel("学号:");
        Id = new JTextField(10);
        studentId.add(studentIdText);
        studentId.add(Id);

        JPanel studentName = new JPanel();
        JLabel nameText = new JLabel("姓名:");
        name = new JTextField(10);
        studentName.add(nameText);
        studentName.add(name);

        queryButton = new JButton("查询");
        addButton = new JButton("添加");

        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
        topPanel.add(major);
        topPanel.add(cla);
//        topPanel.add(Box.createRigidArea(new Dimension(50, 15)));
        topPanel.add(studentId);
        topPanel.add(studentName);
//        topPanel.add(Box.createRigidArea(new Dimension(50, 15)));
        topPanel.add(queryButton); 
        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
        topPanel.add(addButton);

        this.add(topPanel);

        JScrollPane tablePanel = new JScrollPane();
        tablePanel.setPreferredSize(new Dimension(360, 650));
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel) {
            private static final long serialVersionUID = 1L;

            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        for(int i=0;i<tableColName.length;i++){
            tableModel.addColumn(tableColName[i]);
        }
        tablePanel.setViewportView(table);
        this.add(tablePanel);

        queryButton.addActionListener(listener);
        addButton.addActionListener(listener);
        table.addMouseListener(mouseListener);
    }
    
    
    public RollInfo getTableRowRoll(){
        int row = table.getSelectedRows()[0];
        return infoList.get(row);
    }

    @Override
    public void updateTable(List<RollInfo> rollInfos) {
        this.infoList = rollInfos;

       int rows = tableModel.getRowCount();
       for (int i = 0; i < rows; i++) {
           tableModel.removeRow(0);
       }
       
       /*
        * 添加信息
        */
       int bookNum = rollInfos.size();
       for(int i = 0 ; i < bookNum ; i ++){
           RollInfo rollInfo = rollInfos.get(i);
           String[] rollTable = {
                   rollInfo.getStudentNumber(),
                   rollInfo.getName(),
                   rollInfo.getMajor(),
                   rollInfo.getCurGrade(),
                   rollInfo.getIdCard(),
                   rollInfo.getMaritalStatus()
                   };
            
           tableModel.addRow(rollTable);
       }    }

}
